package com.tbsoft.ehr.action.staffFile;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.ServiceCustomer;
import com.tbsoft.ehr.model.StaffInfo;
import com.tbsoft.ehr.model.TransferInfo;
import com.tbsoft.ehr.service.staffTransfer.TransferService;
import com.tbsoft.ehr.service.staffile.StaffFileService;

public class TransferAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//服务客户id
	private String serviceCustomerId;
	//档案对象
	private StaffInfo staff;
	
	//查询条件
	//员工姓名
	private String staffName;
	//调动月份
	private String transferMonth;
	
	private TransferInfo transferInfo;
	private List<TransferInfo> transferLst;
	
	private StaffFileService staffFileService;
	private TransferService transferService;
	
	
	public String getServiceCustomerId() {
		return serviceCustomerId;
	}
	public void setServiceCustomerId(String serviceCustomerId) {
		this.serviceCustomerId = serviceCustomerId;
	}
	
	public String getStaffName() {
		return staffName;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public String getTransferMonth() {
		return transferMonth;
	}
	public void setTransferMonth(String transferMonth) {
		this.transferMonth = transferMonth;
	}
	public StaffInfo getStaff() {
		return staff;
	}
	public void setStaff(StaffInfo staff) {
		this.staff = staff;
	}
	public TransferInfo getTransferInfo() {
		return transferInfo;
	}
	public void setTransferInfo(TransferInfo transferInfo) {
		this.transferInfo = transferInfo;
	}
	public List<TransferInfo> getTransferLst() {
		return transferLst;
	}
	public void setTransferLst(List<TransferInfo> transferLst) {
		this.transferLst = transferLst;
	}
	public TransferService getTransferService() {
		return transferService;
	}
	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}
	
	public StaffFileService getStaffFileService() {
		return staffFileService;
	}
	public void setStaffFileService(StaffFileService staffFileService) {
		this.staffFileService = staffFileService;
	}
	public String add() {
		if(this.staff!=null&&this.staff.getStaffId()!=null) {
			this.staff = this.staffFileService.getStaffFileById(this.staff.getStaffId());
			this.transferLst = this.transferService.getTransfersByStaffId(this.staff.getStaffId());
			return "add";
		} 
		return SUCCESS;
	}
	
	public String save() {
		this.transferInfo.setStaff(this.staff);		
		ServiceCustomer sc= new ServiceCustomer(this.serviceCustomerId);
		this.transferInfo.setSc(sc);
		this.transferService.addTransferInfo(this.transferInfo);
		setRel("staffFile_browse");
		return SUCCESS;
	}
	
	public String query() {
		// 用于分页
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();		

		String transferDateFromStr = null;
		String transferDateToStr = null;
		if (this.transferMonth != null && this.transferMonth.length() > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.MONTH,
					Integer.parseInt(this.transferMonth) - 1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			transferDateFromStr = sdf.format(calendar.getTime());
			calendar.set(Calendar.DAY_OF_MONTH, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH));
			transferDateToStr = sdf.format(calendar.getTime());
		}

		List<StaffInfo> staffLst = null;
		if (this.staffName != null && this.staffName.length() > 0) {
			staffLst = this.staffFileService
					.getStaffFileByStaffName(this.staffName);
			// 输入的姓名没有取到对应的档案，就不去查询调动信息了
			if (staffLst.size() == 0) {
				return "browse";
			}
		}

		if (this.serviceCustomerId != null && !this.serviceCustomerId.isEmpty()) {
			ServiceCustomer sc = new ServiceCustomer(this.serviceCustomerId);
			this.transferInfo.setSc(sc);
		}
		
		int count = this.transferService.getTransferCount(this.transferInfo,staffLst,transferDateFromStr,transferDateToStr);
		setTotalCount(count);
		setPageNumShown((getNumPerPage() + count) / getNumPerPage());

		this.transferLst = this.transferService.getTransfersByCondton(
				this.transferInfo, staffLst, transferDateFromStr,
				transferDateToStr, startIndex, getNumPerPage());
		return "browse";
	}
	
}
